package com.nidu.demo.tenant.model;

import com.alibaba.cola.exception.BizException;
import com.nidu.demo.base.BaseDeletableEntity;
import com.nidu.demo.base.BaseEntity;
import com.nidu.demo.base.BaseStatusEntity;
import com.nidu.demo.common.enums.BooleanEnum;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.util.Set;

/**
 * 租户套餐领域对象
 *
 * @author auto
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class TenantPackage extends BaseEntity implements BaseStatusEntity, BaseDeletableEntity {

    private Long id;

    /**
     * 租户套餐名称
     */
    private String name;

    /**
     * 菜单ID列表
     */
    private Set<Long> menuIds;

    /**
     * 排序值
     */
    private Integer sortOrder;

    /**
     * 状态
     */
    private Integer status;

    /**
     * 是否删除
     */
    private Boolean deleted;

    /**
     * 创建租户套餐
     * @param name 套餐名称
     * @param menuIds 菜单ID列表
     * @param sortOrder 排序值
     * @return TenantPackage实例
     */
    public static TenantPackage create(String name, Set<Long> menuIds, Integer sortOrder) {
        TenantPackage tenantPackage = new TenantPackage();
        tenantPackage.setName(name)
                .setMenuIds(menuIds)
                .setSortOrder(sortOrder);
        return tenantPackage;
    }

    /**
     * 更新租户套餐
     * @param id ID
     * @param name 套餐名称
     * @param sortOrder 排序值
     * @return TenantPackage实例
     */
    public static TenantPackage update(Long id, String name, Integer sortOrder) {
        TenantPackage tenantPackage = new TenantPackage();
        tenantPackage.setId(id)
                .setName(name)
                .setSortOrder(sortOrder);
        return tenantPackage;
    }

    @Override
    public void isDeleted() {
        if (BooleanEnum.TRUE.getValue().equals(this.getDeleted())) {
            throw new BizException(ErrorCodeConstants.TENANT_PACKAGE_NOT_FOUND.getCode(), 
                    ErrorCodeConstants.TENANT_PACKAGE_NOT_FOUND.getMessage());
        }
    }

    @Override
    public void isDisable() {
        if (StatusEnum.DISABLE.getValue().equals(this.status)) {
            throw new BizException(ErrorCodeConstants.TENANT_PACKAGE_STATUS_DISABLE.getCode(), 
                    ErrorCodeConstants.TENANT_PACKAGE_STATUS_DISABLE.getMessage());
        }
    }
}
